<template>
    <div  style="margin: 10px 10px">
      <div style="margin-bottom: 10px">
        <el-input v-model="searchTeaName" placeholder="请输入老师姓名" style="width: 150px;margin-right: 5px"></el-input>
        <el-button type="primary" @click="searchTeaAdmiss">搜索</el-button>
        <el-button type="primary" @click="dialogVisible = true" style="position: absolute;right: 20px">添加</el-button>
      </div>
      <div>
        <el-table :data="admissions" border>
          <el-table-column
            fixed
            prop="teacherName"
            align="center"
            label="老师姓名"
            width="300">
          </el-table-column>
          <el-table-column
            prop="teacherPhone"
            label="老师电话"
            align="center"
            width="300">
          </el-table-column>
          <el-table-column
            prop="admissionName"
            label="负责地区"
            align="center"
            width="250">
          </el-table-column>
          <el-table-column
            prop="gmtCreate"
            label="开始时间"
            align="center"
            width="300">
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="155">
            <template slot-scope="scope">
              <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
              <el-button type="text" size="small" @click="delTeaAdmiss(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @current-change="currentChange"
          :current-page="currentPage"
          layout="prev, pager, next"
          :total="total">
        </el-pagination>
      </div>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        @close="handleColse"
        width="30%">
        <el-form
          label-width="80px"
          :model="admission">
          <el-form-item label="老师姓名">
            <el-input v-model="admission.teacherName" placeholder="请输入老师姓名" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="老师电话">
            <el-input v-model="admission.teacherPhone" placeholder="请输入老师电话" style="width: 200px"></el-input>
          </el-form-item>
          <el-form-item label="负责地区">
            <el-select v-model="admissionName" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.admissionId"
                :label="item.admissionName"
                :value="item.admissionId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="updateAdmission">确 定</el-button>
        </span>
      </el-dialog>
    </div>
</template>

<script>
import { initTeaAdmiss,initAdmiss ,getTeaName ,del, editTeaAdmiss, addTeaAdmiss } from '../../api/admin/admiss'

export default {
  data() {
    return {
      admissionName: '',
      currentPage: 1,
      total: 100,
      admissionId: 0,
      options: [{
        admissionId: 1,
        admissionName: '2'
      }],
      searchTeaName: '',
      admissions: [{
        teacherAdmissId: 0,
        teacherId: 1,
        teacherName: '',
        teacherPhone: '',
        admissionId: 1,
        admissionName: '',
        gmtCreate: ''
      }],
      admission: {
        teacherAdmissId: 0,
        teacherId: 0,
        teacherName: '',
        teacherPhone: '',
        admissionId: 0,
        admissionName: '',
        gmtCreate: ''
      },
      dialogVisible: false
    }
  },
  methods: {
    // 根据老师姓名搜索
    searchTeaAdmiss() {
      if (this.searchTeaName === ''){
        this.currentChange(1)
      } else {
        getTeaName(this.searchTeaName).then(response=>{
          this.admissions = response.data.records
          this.total = response.data.total
        })
      }
    },
    // 打开编辑菜单
    handleClick(row) {
      let old = JSON.stringify(row)
      this.admission = JSON.parse(old)
      this.dialogVisible = true
    },
    handleColse() {
      this.admission = {
        teacherId: 0,
        teacherName: '',
        teacherPhone: '',
        admissionId: 0,
        admissionName: '',
        gmtCreate: ''
      }
    },
    // 获取地区信息
    getAdmiss() {
      initAdmiss().then(response=>{
        console.log(response)
        this.options = response.data
        this.admissionName = response.data[0].admissionName
      })
    },
    // 删除当前列
    delTeaAdmiss(row) {
      console.log(row)
      this.$confirm('此操作将删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.teacherAdmissId).then(response=>{
          console.log(response);
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.currentChange(this.currentPage)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 获取当前页数据
    currentChange(page) {
      this.currentPage = page;
      initTeaAdmiss(this.currentPage).then(response=>{
        console.log(response)
        this.admissions = response.data.records
        this.total = response.data.total
      })
    },
    // 添加或者修改
    updateAdmission(){
      console.log(this.admission)
      this.admission.admissionId = this.admissionName===this.options[0].admissionName?this.options[0].admissionId:this.admissionName
      if (this.admission.teacherId > 0){
        // 修改
        editTeaAdmiss(this.admission).then(response=>{
          console.log(response)
          this.currentChange(this.currentPage)
          this.$message({
            message: response.msg,
            type: 'success'
          })
        })
      } else {
        // 添加
        addTeaAdmiss(this.admission).then(response=>{
          console.log(response)
          this.currentChange(this.currentPage)
          this.$message({
            message: response.msg,
            type: 'success'
          })
        })
      }
      this.dialogVisible =false;
    }
  },
  created() {
    this.getAdmiss()
    this.currentChange(1)
  }
}
</script>

<style scoped>

</style>
